﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Bass.Tools;
using MySql.Data.MySqlClient;

public partial class index : System.Web.UI.Page 
{

    protected void Page_Load(object sender, EventArgs e)
    {
        dlBind();
    }
    public void dlBind()
    {
        string sourceen = Request.QueryString["productsourceen"];
        string kinden = Request.QueryString["productkinden"];
        string beclickedtotalnumber = Request.QueryString["beclickedtotalnumber"];
        string haitao = Request.QueryString["haitao"];
        string tejia = Request.QueryString["tejia"];

        int curpage = Convert.ToInt32(this.lblCurPage.Text);

        //先定义一个PageDataSource,分页全靠它
        PagedDataSource ps = new PagedDataSource();

        //取到连接字符串
        string propertiesPath = Server.MapPath("mysqldb.properties");
        mytools mt = new mytools();
        string ConnString = mt.getMysqlConn(propertiesPath);

        //打开连接
        MySqlConnection myConnection = new MySqlConnection(ConnString);
        myConnection.Open();

        //定义一个MySqlDataAdapter
        MySqlDataAdapter myAdapter = new MySqlDataAdapter();
        if (sourceen != null)
        {
            MySqlDataAdapter da = new MySqlDataAdapter("select productsource from productsource where productsourceen='" + sourceen + "' and sourcestatus='A'", myConnection);
            DataSet ds1 = new DataSet();
            da.Fill(ds1, "productsource_detail");
            DataRowView rowview = ds1.Tables["productsource_detail"].DefaultView[0];
            string source = rowview["productsource"].ToString();
            myAdapter = new MySqlDataAdapter("select * from product_info  where source='" + source + "' and productstatus='B' order by addtime desc", myConnection);
        }
        else if (kinden != null)
        {
            MySqlDataAdapter da = new MySqlDataAdapter("select productkind from productkind where productkinden='" + kinden + "' and kindstatus='A'", myConnection);
            DataSet ds1 = new DataSet();
            da.Fill(ds1, "productkind_detail");
            DataRowView rowview = ds1.Tables["productkind_detail"].DefaultView[0];
            string kind = rowview["productkind"].ToString();
            myAdapter = new MySqlDataAdapter("select * from product_info where productkind='" + kind + "' and productstatus='B' order by addtime desc", myConnection);
        }
        else if (tejia != null)
        {
            myAdapter = new MySqlDataAdapter("select * from product_info where recommendornot='y' and productstatus='B' order by addtime desc", myConnection);
        }
        else if (haitao != null)
        {
            myAdapter = new MySqlDataAdapter("select * from product_info where abroadornot='y' and productstatus='B' order by addtime desc", myConnection);
        }
        else if (beclickedtotalnumber != null)
        {
            myAdapter = new MySqlDataAdapter("select * from product_info where beclickedtotalnumber>0 and productstatus='B' order by beclickedtotalnumber desc", myConnection);
        }
        else
        {
            myAdapter = new MySqlDataAdapter("select * from product_info where productstatus='B' order by addtime desc", myConnection);
        }

        DataSet ds = new DataSet();
        //填充数据
        myAdapter.Fill(ds, "mytable");
        ps.DataSource = ds.Tables["mytable"].DefaultView;
        ps.AllowPaging = true;
        ps.PageSize =20;
        ps.CurrentPageIndex = curpage - 1;
        this.lbtnFirstPage.Enabled = true;
        this.lbtnUpPage.Enabled = true;
        this.lbtnDownPage.Enabled = true;
        this.lbtnLastPage.Enabled = true;
        if (curpage == 1)//已经是第一页
        {
            this.lbtnFirstPage.Enabled = false;
            this.lbtnUpPage.Enabled = false;
        }
        if (curpage == ps.PageCount)//已经是末页
        {
            this.lbtnLastPage.Enabled = false;
            this.lbtnDownPage.Enabled = false;
        }
        this.lblTotalPage.Text = Convert.ToString(ps.PageCount);
        this.Repeater1.DataSource = ps;
        this.Repeater1.DataBind();
        myConnection.Close();

    }

    protected void lbtnFirstPage_Click(object sender, EventArgs e)
    {
        this.lblCurPage.Text = Convert.ToString(1);
        this.dlBind();
    }

    protected void lbtnUpPage_Click(object sender, EventArgs e)
    {
        this.lblCurPage.Text = Convert.ToString(Convert.ToUInt32(this.lblCurPage.Text) - 1);
        this.dlBind();
    }
    protected void lbtnDownPage_Click(object sender, EventArgs e)
    {
        this.lblCurPage.Text = Convert.ToString(Convert.ToUInt32(this.lblCurPage.Text) + 1);
        this.dlBind();
    }
    protected void lbtnLastPage_Click(object sender, EventArgs e)
    {
        this.lblCurPage.Text = this.lblTotalPage.Text;
        this.dlBind();
    }

    protected string Substring(string str, int i, int j)
    {
        mytools mt = new mytools();
        string resultstr = mt.Substring(str,i,j);
        return resultstr;
    }

    protected string ReformDate(string addtime)
    {
        mytools mt = new mytools();
        string datereformed = mt.ReformDate(addtime);
        return datereformed;
    }

}
